#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "log.hpp"
using namespace mylog;
static std::string default_dictpath = "./dict.txt";
static std::string sep = " : ";
class Dict
{
public:
    Dict() {}
    ~Dict() {}
    void Load()
    {
        // 打开文件
        std::fstream in(default_dictpath);
        if (!in.is_open())
        {
            LOG(Level::FATAL) << "file open error";
            exit(1);
        }
        // 读取
        std::string line;
        while (std::getline(in, line))
        {
            // 处理一行信息，建立映射关系
            auto pos = line.find(sep);
            if (pos == std::string::npos)
            {
                LOG(Level::WARNING) << "load error : " << line;
                continue;
            }
            std::string english = line.substr(0, pos);
            std::string chinese = line.substr(pos + 3);
            if (english.empty() || chinese.empty())
            {
                LOG(Level::WARNING) << " unknow : " << line;
                continue;
            }
            _dict.insert(std::make_pair(english, chinese));
            LOG(Level::DEBUG) << "load : " << english << " -> " << chinese;
        }
    }
    std::string Translate(std::string word)
    {
        if (_dict.count(word) == 0)
        {
            return "Unknow";
        }
        return _dict[word];
    }

private:
    std::unordered_map<std::string, std::string> _dict; // 字典
};